import network from '../../utils/network.js';
const app = getApp();
let url = app.globalData.APIURL + '/vending-admin/equipArea/getXzqhList'
let areaList  =[];
network.POST(url).then(() => {
    //请求成功的操作
    areaList  = app.netWorkData.result.areaList;
});
Component({
    properties: {
        showRegion: {
            type: Boolean,
            observer: function (newVal, oldVal) {
                this.setData({
                    dialog: newVal,
                });
            },
        },
        regionValue: {
            type: Array,
            value: [],
            observer: function (newVal, oldVal) {
                if (newVal.length > 0) {
                    let select = -1;
                    for (let i = newVal.length - 1; i >= 0; i--) {
                        if (newVal[i].id !== '') {
                            select = i;
                            break;
                        }
                    }
                    // 除最低级别区（select = 2）以外，需要获取当前级别下一级的数据
                    this.setData({
                        ['region.tabs']: newVal,
                        ['region.select']: select < 2 ? select + 1 : select,
                    }, () => {
                        this.setData({
                            // area: this.getChildArea(select < 2 ? select + 1 : select),
                            area: this.getChildArea(region.tabs.id),
                        });
                    });
                }
            },
        },
    },
    data: {
        dialog: false,
        area: [{parent: 0, name: "河南省", id: 1}],
        region: {
            tabs: [
                {
                    name: '请选择',
                    id: '',
                },
                {
                    name: '请选择',
                    id: '',
                },
                {
                    name: '请选择',
                    id: '',
                },
            ],
            select: 0,
            selectId:''
        },
    },
    methods: {
        // 关闭 picker 触发的方法
        emitHideRegion: function () {
            // if (this.data.region.tabs[2].id === '') {
            //     wx.showToast({
            //         title: '请选择所在地',
            //         icon: 'none',
            //         duration: 2000,
            //     });
            //     return false;
            // }
            let myEventDetail = {}; // detail对象，提供给事件监听函数
            let myEventOption = {}; // 触发事件的选项
            this.setData({
                dialog: !this.data.dialog,
            });
            myEventDetail = {
                showRegion: this.data.dialog,
                regionValue: this.data.region.tabs,
            };
            this.triggerEvent('myevent', myEventDetail, myEventOption);
        },
        bindRegionChange: function (e) {
            console.log(e)
            // 获取当前选中项的name和id并赋值给data中的数据
            console.log(this.data.area);
            let id = 'region.tabs[' + this.data.region.select + '].id';
            let name = 'region.tabs[' + this.data.region.select + '].name';
            this.setData({
                [id]: e.target.dataset.id,
                [name]: e.target.dataset.name,
            });
            let _area = this.getChildArea(e.target.dataset.id);
            console.log(_area)
            // 除了三级以外的需要获取对应子选项
            if (this.data.region.select < 2) {
                this.setData({
                    ['region.select']: ++this.data.region.select,
                    area: _area
                });
            } else {
                // 三级选项选择完毕关闭省市区选择器
                this.emitHideRegion();
            }
        },
        getChildArea: function (id) {
            console.log(id);
            if (!id) {
                // 默认取根路径
                id = 0;
            }
            console.log(id);
            // 根据层级取当前层级下的数据
            let _area = areaList.filter(function (item) {
                return item.parent === id;
            })
            console.log(_area);
            return _area;
        },
        // 省市区tab切换
        changeRegionLevel: function (e) {
            console.log(e);
            let id = e.target.dataset.id;
            let level = e.target.dataset.level;
            // 三级选项的tab点击无效果
            // if (level === 2) return false;
            // 当前选中tab和级别小于当前选中tab的状态都置为初始化状态
            for (let i = level; i < 3; i++) {
                let string = 'region.tabs[' + i + ']';
                this.setData({
                    [string]: {
                        name: '请选择',
                        id: '',
                    },
                });
            }
            this.setData({
                ['region.select']: level,
                ['region.selectId']: id,

            });
            this.setData({
                area: this.getChildArea(level),
            });
        },
    },
});
